package com.burgeon.backend.SHOP.controller;

import com.burgeon.backend.SHOP.entity.Address;
import com.burgeon.backend.SHOP.service.AddressService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("shop/address")
@RestController
public class AddressController extends BaseController {
    @Autowired
    private AddressService addressService;
    //设置为默认地址
    @RequestMapping("setdefault")
    public ResponseResult<Void> SetDefault(String id,HttpSession session){
        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();
        addressService.setDefault(id,uid,token);
        return new ResponseResult<>(SUCCESS);
    }


    @RequestMapping("del")
    public ResponseResult<Void> ModAddress(String id,HttpSession session){
        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();
        addressService.DelAddress(id,uid,token);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("default")
    public ResponseResult<Address> DefaultAddress(HttpSession session){
        if (session.getAttribute("uid")==null){
            return new ResponseResult<>(NOTLOG);
        }
        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();

        Address data = addressService.getDefaultAddress(uid,token);
        return new ResponseResult<Address>(SUCCESS,data);
    }

    @RequestMapping("updata")
    public ResponseResult<Void> ModAddress(Address address){

        addressService.modAddress(address);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("getid")
    public ResponseResult<Address> GetAddressByUser(String id) {

        Address data = addressService.getAddressById(id);
        return new ResponseResult<Address>(SUCCESS, data);
    }

    @RequestMapping("getdata")
    public ResponseResult<List<Address>> GetAddressByUser(HttpSession session){
        if (session.getAttribute("uid")==null){
            return new ResponseResult<>(NOTLOG);
        }
        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();

        List<Address> data = addressService.getAddressByUser(uid,token);
        return new ResponseResult<List<Address>>(SUCCESS,data);
    }

    @RequestMapping("adddata")
    public ResponseResult<Void> AddAddress(Address address,HttpSession session){
        String token = session.getAttribute("token").toString();
        System.err.println(address);
        String uid = session.getAttribute("uid").toString();
        address.setToken(token);
        address.setUid(uid);
        addressService.addAddress(address);
        return new ResponseResult<>(SUCCESS);
    }



    @RequestMapping("testsetdefault")
    public ResponseResult<Void> testSetDefault(String id,String uid){
        addressService.setDefault(id,uid,"lfwd");
        return new ResponseResult<>(SUCCESS);
    }


    @RequestMapping("testdel")
    public ResponseResult<Void> testModAddress(String id,String uid){
        addressService.DelAddress(id,uid,"lfwd");
        return new ResponseResult<>(SUCCESS);
    }


    @RequestMapping("testdefault")
    public ResponseResult<Address> testDefaultAddress(String uid){

        Address data = addressService.getDefaultAddress(uid,"lfwd");
        return new ResponseResult<Address>(SUCCESS,data);
    }

    @RequestMapping("testgetid")
    public ResponseResult<Address> testGetAddress(String id) {

        Address data = addressService.getAddressById(id);
        return new ResponseResult<Address>(SUCCESS, data);
    }

    @RequestMapping("testgetdata")
    public ResponseResult<List<Address>> testGetAddressByUser(String uid){
        List<Address> data = addressService.getAddressByUser(uid,"lfwd");
        return new ResponseResult<List<Address>>(SUCCESS,data);
    }

    @RequestMapping("testadddata")
    public ResponseResult<Void> testAddAddress(Address address,String uid){
        address.setToken("lfwd");
        address.setUid(uid);
        addressService.addAddress(address);
        return new ResponseResult<>(SUCCESS);
    }



}
